<?php
include('../../include/functions.inc.php');
$user = unserialize($_SESSION['user']);

$sql = "select b.CODE,b.NAME from cllg_courses_pensum a inner join
            cllg_courses_catalog b on a.FK_COURSE=b.CODE";
$where = " where a.FK_PENSUM = " . sqlValue(getParam("CODE"), "int");

$existencias = $db->queryToArray($sql . $where);
$total = count($existencias);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Asignaciones de Cursos</title>
        <link href="../../css/ContentStyle.css" rel="stylesheet" type="text/css" />
        <link href="../../js/protocalendar/datepicker.css" rel="stylesheet" type="text/css" />
        <link href="../../css/tableorderer.css" rel="stylesheet" type="text/css" />
        <link href="../../js/dialog/dialog.2.0.css" rel="stylesheet" type="text/css" />
        <script src="../../js/prototype.js" type="text/javascript"></script>
        <script src="../../js/scrpt/scriptaculous.js" type="text/javascript"></script>
        <script src="../../js/jsvalidate.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/prototype-date-extensions.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/datepicker.js"  type="text/javascript"></script>
        <script src="../../js/TableOrderer.js" type="text/javascript"></script>
        <script src="../../js/dialog/dialog.2.0.js" type="text/javascript"></script>
        <script src="../../js/include.js" type="application/javascript"></script>
        <script type="application/javascript">
            var selectedCursos=new Hash();
            var allCursos=new Hash();
            var cursosDeleted=new Hash();

            guardar=function(){
            waitMsg("Guardando...");
            if(selectedCursos.values().length>0 || cursosDeleted.values.length>0){
            $("json").value=selectedCursos.values().toJSON();
            $("jsondel").value=cursosDeleted.values().toJSON();

            $(document.pensum).request({onComplete:function(r){
            closeWait();
            }});
            }else{
            Dialogs.alert("Debe agregar cursos al pensum!");
            }
            }
            addOnLoad(function(){
            var f=new Hash({tittle:'Listado de Catedraticos',width:500,height:500,url:'list.php',callback:selTeach});
            setList($('buscar'),f.toObject());
            });
            seltTeach=function(s){
            $("IDT").value=s.ID;
            $("NAME").value=s.NOMBRES+' '+s.APELLIDOS;	
            }
            addOnLoad(function(){
            waitMsg("Cargando Cursos...");
            new Ajax.Request("listCursos.php",{onComplete:function(r){
            lc=r.responseJSON;
            lc.each(function(e){allCursos.set(e.CODIGO,e)});

            createCourses(allCursos.values(),"listcourses");
            closeWait();
            Droppables.add('courses', {
            onDrop:function(element, dropon, event){
            if(element.id.endsWith("_c")
            || selectedCursos.get(element.id+"_c")!=null
            || cursosDeleted.get(element.id+"_c")!=null){
            return;
            }
            var ob=allCursos.get(element.id);
            var d = new Element('div',{"class":"item"});
            d.id=element.id+"_c";
            d.update(element.innerHTML);
            $("courses").appendChild(d);
            new Draggable(d.id, {revert:true});
            ob.NUEVO=true;
            ob.CICLO=$F("CYCLE");
            selectedCursos.set(d.id,ob);
            }});

            Droppables.add('papelera', {
            onDrop:function(element, dropon, event){
            if(element.id.endsWith("_c")){
            ob=selectedCursos.get(element.id);
            if(!ob.NUEVO){
            cursosDeleted.set(ob.CODIGO,ob);
            }
            selectedCursos.unset(element.id);
            $(element.id).remove();
            }
            }});
            waitMsg("Cargando Pensum...");

            new Ajax.Request("listPensumCursos.php",{
            parameters:'CODE='+$F("CODE"),
            method:'get',
            onComplete:function(r){
            var lc=r.responseJSON;
            // alert(r.responseText);
            lc.each(function(i){
            addCursoAuto(i);
            });
            closeWait();
            }});
            }});
            });

            addCursoAuto=function(c){
            //alert(c);
            var d = new Element('div',{"class":"item"});
            d.id=c.CODIGO+"_c";
            d.update("<img src='../../img/ebook.jpg' width='70' height='65'>"+c.NOMBRE+"</img>");
        $("courses").appendChild(d);
        c.NUEVO=false;
        selectedCursos.set(d.id,c);
        d.hide();
        new Draggable(d.id, {revert:true});
        }

        loadCursos=function(){
        selectedCursos.each(function(i){
        if(i.value.CICLO!=$F("CYCLE")){
        $(i.value.CODIGO+"_c").hide();
        }else{
        $(i.value.CODIGO+"_c").show();
        }
        });
        }

        createCourses=function(list,dest){
        list.each(function(c){
        var d = new Element('div',{"class":"item"});
        var i = new Element('img', { src: '../../img/ebook.jpg',
        width:75, height:69 }).update(c.NOMBRE);
        d.id=c.CODIGO;
        d.appendChild(i);

        d.update("<img src='../../img/ebook.jpg' width='70' height='65'>"+c.NOMBRE+"</img>");
        $(dest).appendChild(d);
        new Draggable(d.id, {revert:true});
        });
        }


    </script>
</head>

<body>
    <form action="../teachers/data.php" method="get" name="pensum">

        <table  width="100%" border="0" cellpadding="0" cellspacing="0" class="tableform">
            <tr>
                <th scope="col">Asignacion de Cursos Catedraticos</th>
                <th scope="col"><input type="button" name="cmdOk2" class="btnOk" id="cmdOk2" value="" onClick="guardar();" /></th>
            </tr>
            <tr>
                <td colspan="2" scope="col"><table width="100%" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="14%">Catedratico</td>
                            <td width="86%"><input name="NOMBRE" type="text" id="NOMBRE" size="60">
                                <input type="button" name="buscar" id="buscar" value="...">
                                <input type="hidden" name="IDT" id="IDT"></td>
                        </tr>

                    </table></td>
            </tr>
            <tr>
                <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

                        <tr>
                            <td align="center"><div id="papelera" style="height:100px; background-color:#996666; color:#FFFFFF">Papelera</div></td>
                        </tr>
                        <tr>
                            <td align="center"><div id="courses" style=" z-index:6; width:100%; 
                                                    height:400px; background-color:#EFEFEF; color:#333333"></div></td>
                        </tr>
                    </table></td>
                <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td>CATALOGO CURSOS</td>
                        </tr>
                        <tr>
                            <td><table width="100%" border="0" cellspacing="2" cellpadding="0">
                                    <tr>
                                        <td width="16%">CODIGO</td>
                                        <td width="42%"><input type="text" name="codigo_curso" id="codigo_curso" /></td>
                                        <td width="42%">NOMBRE</td>
                                        <td width="84%"><input type="text" name="nombre_curso" id="nombre_curso" /></td>
                                    </tr>
                                </table></td>
                        </tr>
                        <tr>
                            <td align="center" id="listcourses"></td></tr>
                    </table></td>
            </tr>
        </table>
    </form>
</body>
</html>
